package com.example.widgethost;

import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class WidgetsRestoredReceiver extends BroadcastReceiver {
    private static final String TAG = "WidgetsRestoredReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (AppWidgetManager.ACTION_APPWIDGET_HOST_RESTORED.equals(intent.getAction())) {
            Log.e(TAG, "WidgetsRestoredReceiver onReceive ACTION_APPWIDGET_HOST_RESTORED");
            int[] oldIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_OLD_IDS);
            int[] newIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
            if (oldIds.length == newIds.length) {
                WidgetHelper widgetHelper = WidgetHelper.getSingleton(context);
                for (int i = 0; i < newIds.length; i++) {
                    widgetHelper.updateWidgetId(oldIds[i], newIds[i]);
                }
            } else {
                Log.e(TAG, "Invalid host restored received " + intent);
            }
        }
    }
}
